package com.gitee.jjnnzb.designpatterninspring.business.login.handler.impl;

import com.gitee.jjnnzb.designpatterninspring.business.login.entity.bo.MobileLoginParam;
import com.gitee.jjnnzb.designpatterninspring.business.login.handler.LoginTemplate;
import com.gitee.jjnnzb.designpatterninspring.business.login.manager.LoginCheckManager;
import com.gitee.jjnnzb.designpatterninspring.business.login.manager.TokenGenerateManager;
import com.gitee.jjnnzb.designpatterninspring.common.enums.LoginVersionEnum;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @author Jjn
 * @since 2023/8/8 14:22
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class MobileLoginTemplateImpl extends LoginTemplate {
    
    // 导入所需要的数据库层资源接口
    private final TokenGenerateManager tokenGenerateManager;
    private final LoginCheckManager loginCheckManager;
    
    @Override
    protected void checkCaptcha(Map<String, Object> map) {
        // skip this step...
        if (log.isInfoEnabled()) {
            log.info("手机端登录，不做图形验证码校验");
        }
    }
    
    @Override
    protected void checkSystemPrivilege(Map<String, Object> map) {
        // do system privilege check...
        if (log.isInfoEnabled()) {
            log.info("校验用户权限");
        }
    }
    
    @Override
    protected void checkLoginIp(Map<String, Object> map) {
        // skip this step...
    }
    
    @Override
    protected void checkUser(Map<String, Object> map) {
        // do userName & password check...
        String userName = (String) map.get("userName");
        if (log.isInfoEnabled()) {
            log.info("查询数据库, 校验用户:{}的个人信息", userName);
        }
       
        loginCheckManager.checkUserAndPassword(map);
        
    }
    
    @Override
    protected void generateToken(Map<String, Object> map) {
        // generate token according to request info...
        tokenGenerateManager.generateToken(map);
    }
    
    @Override
    protected void saveLoginRecord(Map<String, Object> map) {
        // save login record...
        if (log.isInfoEnabled()) {
            log.info("保存手机端登录记录");
        }
    }
    
    @Override
    protected void analyseRequestInfo(Map<String, Object> map) {
        if (log.isInfoEnabled()) {
            log.info("接收到手机端登录请求");
        }
        MobileLoginParam params = (MobileLoginParam) map.get("params");
        map.put("userName", params.getUserName());
        map.put("systemCode", params.getSystemCode());
    }
    
    @Override
    protected String getVersion() {
        return LoginVersionEnum.MOBILE_V1.getVersion();
    }
}
